﻿using System.Linq;
using CustomFormToolkit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NuPattern.Runtime.ToolkitInterface;
using NuPattern.VisualStudio.Solution;

namespace MVCAppToolkit.UserTests
{
    public class CreateCustomFormSpec
    {
        // Tests incomplete
        [Ignore]
        [TestClass]
        public class GivenACustomForm : CreateMvcApplicationSpec.GivenSolutionBuilder
        {
            private IProject mvcAppProject;
            private IMvcApplication mvcAppPattern;
            private ICustomForm customForm;

            [TestInitialize]
            public override void InitializeContext()
            {
                base.InitializeContext();

                this.mvcAppProject = solution.Find<IProject>(p => p.Name == this.projectName).First();
                this.mvcAppPattern = this.patternManager.Products.FirstOrDefault(p => p.InstanceName == this.projectName).As<IMvcApplication>();

                // Create extension pattern
                var controller = this.mvcAppPattern.DefaultView.Controllers.CreateController("Controller1");
                var action = controller.Actions.CreateAction("Action1");
                this.customForm = action.CreateViewForm("Form1", CustomFormToolkitInfo.ProductId, CustomFormToolkitInfo.VsixIdentifier).As<ICustomForm>();
            }

            [TestCleanup]
            public override void Cleanup()
            {
                base.Cleanup();
            }

            [HostType("VS IDE")]
            [TestMethod, TestCategory("Integration")]
            public void ThenFormElementCreated()
            {
                Assert.IsNotNull(this.customForm);
            }

            [HostType("VS IDE")]
            [TestMethod, TestCategory("Integration")]
            public void ThenProjectItemCreatedAndLinked()
            {
                var formItem = this.mvcAppProject.Find<IItem>(item => item.GetLogicalPath() == this.projectName + "/Views/Controller1/Form1").First();
                Assert.IsNotNull(formItem);

                Assert.IsTrue(customForm.References.Any(r => r.Value == string.Format("solution://{0}/{1}", this.mvcAppProject.Id, formItem.Id)));
            }
        }
    }
}
